package dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import dao.tUserDao;
import entity.tUser;

public class tUserDaoImpl extends HibernateDaoSupport implements tUserDao {

	public List<tUser> getTUser(String hql) {
		List<tUser> users = this.getHibernateTemplate().find(hql);
		if(users!=null&&users.size()>0){
			return users;
		}else{
			return null;
		}
	}
	
	public void add(tUser tuser) {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().save(tuser);

	}

	public void delete(String userId) {
		
//		tUser tuser = new tUser();
//		tuser.setUserId(userId);
//		
	System.out.println(userId);
//		System.out.println(tuser);
//		//String hql = "from [greentruss].[dbo].[tUser] where userId = " + "'" + userId + "'";
//		
//		this.getHibernateTemplate().delete(tuser);
	
	this.getHibernateTemplate().delete(getHibernateTemplate().get(tUser.class,new String(userId)));
		
		//this.delete( (String) getHibernateTemplate().get(tUser.class,userId));

	}

	@SuppressWarnings("unchecked")
	public List<tUser> search(String userName) {
		// TODO Auto-generated method stub
		String hql = "from tUser where userName like " + "'%" + userName + "%'";
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tUser> searchuserName(String userName){
		
		String hql = "from tUser where userName = " + "'" + userName + "'";
		
		return this.getHibernateTemplate().find(hql);
	}

	public void update(tUser tuser) {
		// TODO Auto-generated method stub
		
		
		this.getHibernateTemplate().update(tuser);

	}
	
	@SuppressWarnings("unchecked")
	public List<tUser> searchById(String userId){
		
		String hql = "from tUser where userId = " + "'" + userId + "'";
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tUser> searchAlltUser(){
		
		String hql = "from tUser order by addTime desc";
		
		return this.getHibernateTemplate().find(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<tUser> searchByGrade(){
		
		String hql = "from tUser where grade = 1";
		return this.getHibernateTemplate().find(hql);
	}
	
	public List queryForPage(final String hql,final int offset,final int length){
        List list = getHibernateTemplate().executeFind(new HibernateCallback(){
            public Object doInHibernate(Session session) throws HibernateException,SQLException{
                Query query = session.createQuery(hql);
                query.setFirstResult(offset);
                query.setMaxResults(length);
                List list = query.list();
                System.out.println(query.list());
                return list;
            }
        });
        return list;
    }
	
	 public int getAllRowCount(String hql){
	        return getHibernateTemplate().find(hql).size();
	    }



}
